layui.use(['table', 'admin', 'ax','laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    laydate.render({
      elem: '#startTime'
        	,value:new Date()
    });

    laydate.render({
      elem: '#endTime'
        	,value:new Date()
    });


    /**
     * 货运订单表管理
     */
    var OrderTransport = {
        tableId: "orderTransportTable"
    };

    /**
     * 初始化表格的列
     */
    OrderTransport.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'setMentiontime', sort: true, title: '发货日期', style:'font-weight: 600;'},
            {field: 'getUnit', title: '收货单位', style:'font-weight: 600;',width:180},
            {field: 'getLocation', title: '收货地址' ,width:250},
            {field: 'kilometer', title: '公里数', totalRowText: '合计：'},
            {field: 'longitude', title: '经度', hide: true},
            {field: 'latitude', title: '维度', hide: true},
            {field: 'goodsNum', title: '商品数', totalRow: true},
            {field: 'totalNum', title: '数量', totalRow: true},
            {field: 'totalVolume', title: '体积', totalRow: true},
            
            {field: 'operateName', sort: true, title: '项目'},
            {field: 'customerName', sort: true, title: '客户'},
            

            {field: 'lineName', sort: true, title: '线路'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    OrderTransport.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        queryData['operateId'] = $("#operateId").val();
        queryData['customerId'] = $("#customerId").val();
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        table.reload(OrderTransport.tableId, {
            where: queryData, page: {curr: 1},
            done:function () {
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + OrderTransport.tableId,
        url: Feng.ctxPath + '/orderTransport/orderTransportCount',
        page: true,
        limit: 60,
		limits:[40,60,120,240,360,480,1200],
        height: "full-60",
        cellMinWidth: 100,
        toolbar: '#toolbarDemo', //开启头部工具栏，并为其绑定左侧模板
        totalRow: true ,//开启合计行
        defaultToolbar: ['filter', 'exports', 'print'],
        cols: OrderTransport.initColumn()
    });



    /**
     * 导出excel按钮
     */
    OrderTransport.exportExcel = function () {
        var checkRows = table.checkStatus(OrderTransport.tableId);
        var originalColumns = OrderTransport.initColumn()[0];
        var dataToExport = checkRows.data;
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            // table.exportFile(tableResult.config.id, checkRows.data, 'xls');
            dataToExport = dataToExport.map(function (row) {
                delete row.id; // 移除不需要的字段
                delete row.center;
                return row;
            });
            // 创建新的 columns 数组，只包含需要导出的列
            var exportColumns = originalColumns.filter(column => {
                // 排除表头
                return column.field !== 'id' && column.field !== 'center';
            });

            layer.confirm('请选择要导出格式：', {
                btn: ['xlsx', 'xls'],
                skin: 'layui-layer-molv'
            }, function (index) {
                tableToExcel(dataToExport, exportColumns, '运单订单.xlsx');
                layer.close(index);
            }, function (index) {
                table.exportFile(tableResult.config.id, checkRows.data, 'xls')
                layer.close(index);
            })
        }
    };


    $('#btnExp').click(function (){
        OrderTransport.exportExcel();
    })


    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        OrderTransport.search();
    });

    // 单击行事件
    table.on('row(' + OrderTransport.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + OrderTransport.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + OrderTransport.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6" });//改变当前tr颜色
    });

});
